package com.zwy.rxjava2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zwy.rxjava.LogUtils;
import com.zwy.rxjava.R;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

public class RxFilterActivity extends Activity implements View.OnClickListener {

	private TextView mText;
	private Button mBtn;
	private TextView mEdit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout1);

		LogUtils.allowD = true;
		LogUtils.allowE = true;
		LogUtils.customTagPrefix = "itchen";

		initView();
	}

	private void initView() {
		mText = (TextView) findViewById(R.id.text1);
		mEdit = (TextView) findViewById(R.id.edit1);
		mBtn = (Button) findViewById(R.id.button);

		mBtn.setOnClickListener(this);
		mText.setOnClickListener(this);
		mEdit.setOnClickListener(this);
		mEdit.setText("输入1-10,过滤掉能被2整除的数");
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.text1:
				break;
			case R.id.edit1:
				break;
			case R.id.button:

				start();
				break;
		}
	}

	private void start() {
		Integer[] integers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

		Observable.from(integers)
				.filter(new Func1<Integer, Boolean>() {
					@Override
					public Boolean call(Integer integer) {
						return integer % 2 != 0;//过滤掉偶数
					}
				})
				.subscribe(new Action1<Integer>() {
					@Override
					public void call(Integer integer) {
						mText.append(integer.toString() + ",");
					}
				});
	}
}
